﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CSharp.Libs.ExceptionHandling;
using CSharp.Libs.Koc;
using KocMapView;
using System.Collections;

namespace KocAllianceLister
{
    /// <summary>
    /// Interaktionslogik für AllianceMembers.xaml
    /// </summary>
    public partial class AllianceMembers : Window
    {
        AllianceMembersViewModel _AMVM = null;
        wndKocMapView _KMV = null;
        KocMapViewViewModel _KMVVM = null;

        public AllianceMembers(
            ExceptionCollector EC,
            URLHolder UH,
            Int32 AllianceId,
            String AllianceName,
            Boolean ExecuteViewCourt)
        {
            InitializeComponent();

            _AMVM = new AllianceMembersViewModel(EC, UH, AllianceId, AllianceName, ExecuteViewCourt);
            DataContext = _AMVM;
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if(_KMV == null)
            {
                _KMV = new wndKocMapView();
                _KMV.Closed += new EventHandler(_KMV_Closed);
            }
            _KMVVM = _KMV.DataContext as KocMapViewViewModel;

            if (_KMVVM != null)
            {
                ArrayList aryCoords = new ArrayList();
                foreach (KocAllianceMembersListItem KAMLI in _AMVM.AllianceMembers)
                {
                    foreach (KocAllianceMembersCityItem KAMCI in KAMLI.Cities)
                    {
                        try
                        {
                            aryCoords.Add(
                                new KoordItem(
                                    KAMCI.XCoord + "," + KAMCI.YCoord,
                                    KAMLI.DisplayName,
                                    KAMLI.Might,
                                    KAMLI.AllianceName,
                                    KAMCI.CityName,
                                    KAMLI.Online,
                                    KAMLI.LastLogin,
                                    Brushes.Black));
                        }
                        catch 
                        {
                        }
                    }
                }

                _KMVVM.ShowCoords(aryCoords);

                _KMV.Show();
            }
        }

        void _KMV_Closed(object sender, EventArgs e)
        {
            _KMV.Closed -= _KMV_Closed;
            _KMV = null;
        }
    }
}
